﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Media.Imaging;
using WpfApplication1.Models;
using WpfApplication1.Commands;

namespace WpfApplication1.ViewModel
{
    class NotificationViewModel : INotifyPropertyChanged
    {
        NotificationModel notification;
        private static WindowDetail wnd_detail = null;
        RootData rootdata = new RootData();
        CFacebookServer fbServer = new CFacebookServer(MainWindow.AccessToken);

        public string Title
        {
            get { return notification.Title; }
            set
            {
                notification.Title = value;
                OnPropertyChanged("Title");
            }
        }

        public string Time
        {
            get { return notification.Time; }
            set
            {
                notification.Time = value;
                OnPropertyChanged("Time");
            }
        }

        public bool IsUnread
        {
            get { return notification.IsUnread; }
            set
            {
                notification.IsUnread = value;
                OnPropertyChanged("IsUnread");
            }
        }

        public BitmapImage ProfilePicture
        {
            get
            {
                BitmapImage bm = new BitmapImage();
                bm.BeginInit();
                string urlPic = string.Format("https://graph.facebook.com/{0}/picture?type={1}", notification.FromId, "square");
                bm.UriSource = new Uri(urlPic);
                bm.EndInit();
                return bm;
            }
        }

        public Command ClickCommand
        {
            get
            {
                return new Command(DoLeftButtonClick);
            }
        }

        private void DoLeftButtonClick()
        {
            string postid = "";
            string link = notification.Link;
            IsUnread = false;
            if (link.Contains("post"))
            {
                try
                {
                    link = link.Replace("posts/", "#");
                    string[] temp = link.Split("#".ToCharArray());
                    postid = temp[1];
                    //postid = notification.ToId + "_" + postid;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                IDictionary<string, object> tmp = (IDictionary<string, object>)fbServer.Get(postid);
                tmp = (IDictionary<string, object>)tmp["from"];
                string tmp_id = (string)tmp["id"];
                postid = tmp_id + "_" + postid;

            }
            else if (link.Contains("photo.php"))
            {
                return;
                link = link.Replace("photo.php?fbid=", "#");
                string[] temp = link.Split("#".ToCharArray());
                string[] temp2 = temp[1].Split("&".ToCharArray());
                postid = temp2[0];
            }
            else
            {
                System.Diagnostics.Process.Start(link);
                return;
            }
            if (wnd_detail != null)
            {
                wnd_detail.Close();
            }
            wnd_detail = new WindowDetail(postid, (string)rootdata.personalInfo["id"], (string)rootdata.personalInfo["name"], 100, 330, MainWindow.AccessToken);
            wnd_detail.Show();
        }

        public NotificationViewModel(string id, string fromid, string toid, string titletext, string link, string time, bool isUnread)
        {
            notification = new NotificationModel(id, fromid, toid, titletext, link, time, isUnread);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
